package com.example.zhish.explore_demo001;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener,View.OnCreateContextMenuListener,AdapterView.OnItemSelectedListener {
    private TextView nowPath;
    private ListView listView1;
    private List<Explore_item> list;
    private MyAdapter myAdapter;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Global.global_init(MainActivity.this);
        initView();
        initListener();
        initValues();

        listView1.setAdapter(myAdapter);


    }

    public void initValues() {
        list = new ArrayList<>();

        File sd = Environment.getExternalStorageDirectory();
        loadFile(sd);
        myAdapter = new MyAdapter(list, MainActivity.this);
    }

    //视图初始化
    private void initView() {
        nowPath = (TextView) findViewById(R.id.nowPath);
        listView1 = (ListView) findViewById(R.id.listview1);
    }

    //监听器初始化
    private void initListener() {

        listView1.setOnItemClickListener(this);
        listView1.setOnCreateContextMenuListener(this);
        listView1.setOnItemSelectedListener(this);
    }




    /**
     * 每个列表项的点击事件
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Explore_item explore_item = list.get(position);
        if (explore_item.isItem_isDir()) {
            list.clear();
            loadFile(new File(explore_item.getItem_filePath()));
            myAdapter.notifyDataSetChanged();
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("文件详情");
            builder.setMessage("文件名：" + explore_item.getItem_file() + "\r\n" + "文件大小：" + new File(explore_item.getItem_filePath()).length() + "  Byte");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            builder.create().show();
        }


    }

    public void loadFile(File file) {
        Explore_item explore_item;
        File[] files = file.listFiles();
        String extra_name;
        nowPath.setText("当前路径为：" + file.getAbsolutePath());
        Log.i("mike", "--" + Environment.getExternalStorageDirectory());
        Log.i("mike", "--" + file.getAbsolutePath());

        if (!Environment.getExternalStorageDirectory().toString().equals(file.getAbsolutePath().toString())) {
            explore_item = new Explore_item();
            explore_item.setItem_extraName("open_dir");
            explore_item.setItem_file("返回上一级");
            explore_item.setItem_filePath(file.getParent());
            explore_item.setItem_icon(Global.images.get("open_dir"));
            explore_item.setItem_isDir(true);
            explore_item.setItem_quit_or_not(false);
            list.add(explore_item);

        }
        for (int i = 0; i < files.length; i++) {
            explore_item = new Explore_item();
            explore_item.setItem_file(files[i].getName());
            explore_item.setItem_filePath(files[i].getAbsolutePath());
            if (files[i].isDirectory()) {
                explore_item.setItem_isDir(true);
                explore_item.setItem_extraName("close_dir");
                extra_name = "close_dir";
            } else {
                extra_name = files[i].getName().substring(files[i].getName().lastIndexOf(".") + 1).toLowerCase();
                switch (extra_name) {
                    case "mp3":
                    case "mp4":
                    case "txt":
                    case "jpg":
                    case "bmp":
                    case "png":
                    case "gif":
                        explore_item.setItem_extraName(extra_name);
                        break;
                    default:
                        explore_item.setItem_extraName("unKonw");
                        extra_name = "unKonw";
                        break;
                }
                explore_item.setItem_isDir(false);
            }

            explore_item.setItem_icon(Global.images.get(extra_name));
            explore_item.setItem_quit_or_not(true);


            list.add(explore_item);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Explore_item explore_item = list.get(0);
            if(!explore_item.isItem_quit_or_not()){
                listView1.performItemClick(listView1.getChildAt(0),0,listView1.getChildAt(0).getId());
            }else{
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("提示信息");
                builder.setMessage("确定要离开吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();

            }
            //将返回功能取消
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     *
     * @param menu
     * @param v
     * @param menuInfo
     */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0,0,0,"复制");
        menu.add(0,1,0,"粘贴");
        menu.add(0,2,0,"移动");

    }

    //实现菜单选项的逻辑功能
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo adapterContextMenuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        Log.i("mike",adapterContextMenuInfo.toString());
        Log.i("mike","--"+adapterContextMenuInfo.id+"");

        switch (item.getItemId()){
            case 0:
                Toast.makeText(MainActivity.this,"复制",Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(MainActivity.this,"粘贴",Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(MainActivity.this,"移动",Toast.LENGTH_SHORT).show();
                break;

        }

        return super.onContextItemSelected(item);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        Log.i("mike",list.get(position).getItem_extraName());
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
